import Block from '../basics/Block.js'
import Phere from '../basics/Phere.js'
import Row from '../basics/Row.js'
/**
 * 类比 与 转换
 * 块和文件互转
 * 可以有转换转换规则
 *
 */
export default class AnalogyChange{



    static toBlock(phere,rule){

      let square=new Block();
        if(!rule){
            for(let i=0;i<phere.list.length;i++){
                let data=new Row(0,0,0,9);
                square.addRow(data)
                let list=phere.list[i];
                for(let l=0;l<list.list.length;l++){
                    let row=list.list[l];
                    if(row.find(1)===null||row.isValue(1,"")){ //0
                        if(row.find(2)===null||row.isValue(2,"")){//0,0,
                            if(row.find(3)===null||row.isValue(3,"")){
                                data=new Row(row.find(4),1);
                            } else {
                                data=new Row(row.find(3),row.find(4),5);
                            }
                        } else {//0,xx,
                            if(row.find(3)===null||row.isValue(3,"")){
                                data=new Row(row.find(2),row.find(4),3);
                            } else {
                                data=new Row( row.find(2),row.find(3),row.find(4),4);
                            }
                        }

                    }else { // 1
                        if(row.find(2)===null||row.isValue(2,"")){ //xx,0,
                            if(row.find(3)===null||row.isValue(3,"")){
                                data=new Row(row.find(1),row.find(4),2);
                            } else {
                                data=new Row(row.find(1),row.find(3),row.find(4),6);
                            }
                        } else {//xx,x,
                            if(row.find(3)===null||row.isValue(3,"")){
                                data=new Row(row.find(1),row.find(2),row.find(4),3);
                            } else {
                                data=new Row(8);
                                square.addRow(data)
                                data=row ;
                            }
                        }
                    }

                    square.addRow(data)

                }
            }
        }

        return square;
    }

    static toPhere(square,rule){
        let phere=new Phere();

        if(!rule){
            let row =  square.list[0];
            if(row.find(4)!="9"){
                phere.addData(null,null,null,new Block())
            }
            for(let i=0;i<square.list.length;i++){
               let row =  square.list[i];

                switch(row.find(4)) {
                    case '1': //  0,0,0,xx
                        phere.addData(0,phere.list.length-1,null,new Row(0,0,0,row.find(1)))
                        break;
                    case '2'://  xx,0,0,xx
                        phere.addData(0,phere.list.length-1,null,new Row(row.find(1),0,0,row.find(2)))
                        break;
                    case '3':// xx,xx,0,xx
                        phere.addData(0,phere.list.length-1,null,new Row(row.find(1),row.find(2),0,row.find(3)))
                        break;
                    case '4'://0,xx,xx,xx
                        phere.addData(0,phere.list.length-1,null,new Row(0,row.find(1),row.find(2),row.find(3)))
                        break;
                    case '5'://0,0,xx,xx
                        phere.addData(0,phere.list.length-1,null,new Row(0,0,row.find(1),row.find(2)))
                        break;
                    case '6'://  xx,0,xx,xx
                        phere.addData(0,phere.list.length-1,null,new Row(row.find(1),0,row.find(2),row.find(3)))
                        break;
                    case '7'://  0,xx,0,xx
                        phere.addData(0,phere.list.length-1,null,new Row(0,row.find(1),0,row.find(2)))
                        break;
                    case '8'://  xx,xx,xx,xx
                        let rowData=   square.list[i+1];
                        phere.addData(0,phere.list.length-1,null,rowData)
                        i++;
                        break;
                    case '9':   default://
                        phere.addData(0,null,null,new Block());
                        break;
                 }
            }
        }
        return phere;
    }

    static findRuel(){

    }

}